package practice;

/**
 * 给你一个 32 位的有符号整数 x ，返回将 x 中的数字部分反转后的结果。
 *
 * @Auther Jun jie Yi
 * @Date 16:02 2022/1/11
 */
public class LeetCode7 {
    public static void main(String[] args) {
        System.out.println(reverse(1534236469));
    }

    public static int reverse(int x) {
        double n = 0;
        while (x != 0) {
            n = n * 10 + x % 10;
            x = x / 10;
        }
        return (int) n == n ? (int) n : 0;
    }

    public static int reverse1(int x) {
        int t = x, n = 0;
        int sum = 0;
        while (t != 0) {
            n++;
            t = t / 10;
        }
        //拿到最低位
        while (x != 0) {
            int r = x % 10;
            n--;
            sum = sum + r * (int) Math.pow(10, n);
            if (((x > 0 && sum < 0) || (x < 0 && sum > 0)) || (r > 2 && n >= 9) || (r < -2 && n >= 9)) {
                return 0;
            }
            x = x / 10;
        }
        return sum;
    }
}
